<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use api\wxapp\model\Activity;
class ActivityController extends AdminBaseController
{
    public function index()
    {
        $status = 0;
        $where = array();
        $params = $this->param();

        if(isset($params['name']) && $params['name']){
            $where[] = ['name','like','%'.$params['name'].'%'];
        }
        if(isset($params['status']) && $params['status']){
            $where[] = ['status','eq',$params['status']];
            $status = $params['status'];
        }
        $list = Activity::where($where)->paginate(10);
        $this->assign('status',$status);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    //添加活动
    public function add()
    {
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
                'city' => 'require',
                'content' => 'require',
                'img' => 'require',
                'num' => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $_POST['img'] = '/upload/'.$_POST['img'];
            unset($_POST['photo_name']);
            $_POST['addtime'] = date('Y-m-d H:i:s',time());
            $res = Activity::insert($_POST);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
    public function edit()
    {
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
                'city' => 'require',
                'content' => 'require',
                'img' => 'require',
                'num' => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            if($_POST['img']){
                $_POST['img'] = '/upload/'.$_POST['img'];
            }
            unset($_POST['photo_name']);
            $res = Activity::update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $params = $this->param();
        $info = Activity::get($params['id']);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function delete()
    {
        $params = $this->param();
        $res = Activity::destroy($params['id']);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }

    public function lst()
    {
        $params = $this->param();
        $where = array();
        if(isset($params['id'])){
            $where[] = ['t2.activity_id','eq',$params['id']];
        }
        if(isset($params['name'])){
            $where[] = ['t1.name','like','%'.$params['name'].'%'];
        }
        $list = Activity::alias('t1')
            ->join('app_activity_list t2','t2.activity_id = t1.id')
            ->join('app_user t3','t3.id = t2.uid')
            ->field('t3.nickname,t1.name,t2.addtime,t2.id')
            ->where($where)
            ->paginate(10);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
}
